﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;

namespace System.Web.Mvc.Ajax
{
    /// <summary>
    /// “更多”分页方式的Ajax分页控件生成类
    /// </summary>
    public class AjaxMorePaggerFormat : AjaxPaggerFormat
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="options">AjaxHelper.AjaxOptions对象</param>
        public AjaxMorePaggerFormat(AjaxOptions options)
            : base(options)
        {
            this.MoreLabel = "more";
        }

        /// <summary>
        /// “更多”按钮Label，默认值：more
        /// </summary>
        public string MoreLabel { get; set; }

        /// <summary>
        /// 生成HttpGet方式的分页控件
        /// </summary>
        /// <param name="actionName">获取分页数据的Action Name</param>
        /// <param name="controllerName">获取分页数据的Controller Name</param>
        /// <param name="routeValues">获取分页数据的路由参数</param>
        /// <param name="rowCount">分页的总行数</param>
        /// <param name="pageIndex">当前分页索引，从0开始</param>
        /// <param name="pageSize">每页显示个数</param>
        /// <returns></returns>
        public override string GenerateGetFormatString(string actionName, string controllerName, RouteValueDictionary routeValues, int rowCount, int pageIndex, int pageSize)
        {
            int pageCount = pageSize != 0 ? (rowCount % pageSize == 0 ? rowCount / pageSize : rowCount / pageSize + 1) : 0;
            if (pageCount <= 1)
            {
                return String.Empty;
            }
            UrlHelper urlHelper = new UrlHelper(this.Helper.ViewContext.RequestContext);
            string formAction = urlHelper.Action(actionName, controllerName, routeValues);
            formAction = urlHelper.Content(formAction);
            StringBuilder sb = new StringBuilder();
            
            if (this.Options.InsertionMode == InsertionMode.Replace)
            {
                this.Options.InsertionMode = InsertionMode.InsertAfter;
            }
            using (AjaxForm form = new AjaxForm(this.Helper, formAction, this.Options, null, sb, false))
            {
                form.BeginChild("script", new Dictionary<string, string>() {
                    { "type", "text/javascript" }
                });
                StringBuilder sbScript = new StringBuilder();

                sbScript.AppendLine("function " + form.FormId + "$GetMore() {");
                sbScript.AppendLine("   var $_pageIndex_$ = $('#" + form.FormId + "').find(':hidden[name=pageIndex]:first').val();");
                sbScript.AppendLine("   $_pageIndex_$ = !isNaN($_pageIndex_$) ? parseInt($_pageIndex_$) + 1 : 0;");
                sbScript.AppendLine("     $('#" + form.FormId + "').find(':hidden[name=pageIndex]:first').val($_pageIndex_$);$('#" + form.FormId + "').submit();");
                sbScript.AppendLine("};");

                form.AppendHtml(sbScript.ToString());

                form.EndChild("script");

                form.AppendChild("input", new Dictionary<string, string>() { 
                    { "type", "hidden" }, 
                    { "name", "pageIndex" },
                    { "value", pageIndex.ToString() }
                });

                HttpRequest request = System.Web.HttpContext.Current.Request;
                if (request.Form.HasKeys())
                {
                    foreach (string key in request.Form.Keys)
                    {
                        if (key != "pageIndex")
                        {
                            form.AppendChild("input", new Dictionary<string, string>() { 
                                { "type", "hidden" }, 
                                { "name", key },
                                { "value", request[key] }
                            });
                        }
                    }
                }

                form.BeginChild("a", new Dictionary<string, string>() {                    
                    { "id", form.FormId + "_more" },
                    { "href", "#" + form.FormId + "_more" },
                    { "onclick", form.FormId + "$GetMore();" }
                });
                form.AppendHtml(this.MoreLabel);
                form.EndChild("a");
            }

            return sb.ToString();
        }

        /// <summary>
        /// 生成HttpPost方式的分页控件
        /// </summary>
        /// <param name="actionName">获取分页数据的Action Name</param>
        /// <param name="controllerName">获取分页数据的Controller Name</param>
        /// <param name="routeValues">获取分页数据的路由参数</param>
        /// <param name="rowCount">分页的总行数</param>
        /// <param name="pageIndex">当前分页索引，从0开始</param>
        /// <param name="pageSize">每页显示个数</param>
        /// <returns></returns>
        public override string GeneratePostFormatString(string actionName, string controllerName, RouteValueDictionary routeValues, int rowCount, int pageIndex, int pageSize)
        {
            return GenerateGetFormatString(actionName, controllerName, routeValues, rowCount, pageIndex, pageSize);
        }
    }
}
